package com.chujian.framework.ws.support.rcv;

import com.chujian.framework.ws.WsMsg;
import com.chujian.framework.ws.IWsRcvMsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.PriorityOrdered;

/**
 * 处理响应，模拟请求响应
 */
@Slf4j
public class RcvMsgRespHandler implements IWsRcvMsgHandler, PriorityOrdered {

    private final RcvMsgRespDataCenter msgRespDataCenter;

    public RcvMsgRespHandler(RcvMsgRespDataCenter msgRespDataCenter) {
        this.msgRespDataCenter = msgRespDataCenter;
    }

    @Override
    public boolean canHandle(WsMsg msg, String payload) { // 如果是响应
        return WsMsg.MSG_TYPE_RESP.equals(msg.getMsgType());
    }

    @Override
    public WsMsg handle(WsMsg msg, String payload) {
        if (StringUtils.isNotEmpty(msg.getReqId())) {
            msgRespDataCenter.addResp(msg.getReqId(), msg);
        }
        return null;
    }

    @Override
    public int getOrder() {
        return HandlerOrderDef.DEAL_RESP;
    }
}
